#include <iostream>
#include <queue>
using namespace std;

int N; priority_queue<int, vector<int>, greater<int>> q;
void solve();

int main() {
    cin >> N;
    for (int i = 0; i < N; i++) {
        int temp;
        cin >> temp;
        q.push(temp);
    }
    solve();
    return 0;
}


void solve() {
    int res = 0;
    while (q.size() > 1) {
        int l1 = q.top(); q.pop();
        int l2 = q.top(); q.pop();
        res += l1 + l2;
        q.push(l1 + l2);
    }
    cout << res << endl;
}